<?php

namespace app\model;

use app\stateData;
use think\facade\Log;
use think\Model;
use think\model\concern\SoftDelete;

class ClueCommunicateRecord extends Model
{
    /**
     * 人才沟通记录表
     */
    protected $createTime='create_date';
    use SoftDelete;
    protected $deleteTime = 'delete_time';


    public function user(): \think\model\relation\BelongsTo
    {
        return $this->belongsTo(User::class,'communicate_uid','id')->bind(['username']);
    }

    public function getIntentionAttr($name): array
    {
        return [
                'id'=>$name?:'',
                'text'=>stateData::$_intention[$name]?:''
            ];
    }


    public function getCallAttr($name):array
    {
        return [
            'id'=>$name?:'',
            'text'=>stateData::$_call[$name]?:''
        ];
    }

    public function getCommunicateUidAttr($name)
    {
        return [
            'id'=>$name,
            'text'=>(new User)->getIdNickName ($name)
        ];
    }
    public function getNextCommunicateDateAttr($name)
    {
        if ($name == '0000-00-00 00:00:00'){
            return '';
        }

        return $name;
    }

    public function getCommunicateDateAttr($name)
    {
        if ($name == '0000-00-00 00:00:00') return '';

        return $name;
    }

//    public function setCallAttr( $name)
//    {
//        return array_search ($name, stateData::$_call, true);
//    }
    /**
     * 批量添加人才沟通记录
     * @param $data
     * @return \think\Collection
     * @throws \Exception
     */
    public function addAllPersonCommunicateRecordModel($data): \think\Collection
    {
        return $this->saveAll ($data);
    }

    /**
     * 单个添加人才沟通记录
     * @param $data
     * @return \app\model\ClueCommunicateRecord|\think\Model
     */
    public function addPersonCommunicateRecordModel($data){
        return self::create ($data);
    }
    /**
     * 修改沟通记录
     * @param $data
     * @return \app\model\ClueCommunicateRecord
     */
    public function editCommunicate($data)
    {
        return self::update ($data);
    }

    public function editIdCommunicate($id,$data)
    {
        return $this->where (['id'=>$id])->update ($data);
    }

    /**
     * 获取该人才沟通记录
     * @param $id
     * @param $type
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getPersonComRecordModel($id,$type=1): array
    {
        return $this->where (['clue_p_id'=>$id,'type'=>$type])->select()->toArray ();
    }

    /**
     * 獲取最新一条沟通记录
     * @param $id
     * @param $type
     * @return array|null
     */

    public function getPersonIpToDateComRecordModel($id,$type=1): ?array
    {
        $data = $this->where (['clue_p_id'=>$id,'type'=>$type])->order ('id DESC' )->findOrEmpty();

        if (!$data->isEmpty ()){
            return $data->toArray ();
        }
        return [];
    }

    /**
     * 删除沟通记录
     * @param $id
     * @return bool
     */
    public function delPersonCommunicateRecordModel($id): bool
    {
        return self::destroy ($id);
    }

    /**
     * 自动调回
     * @param $where
     * @return \app\model\ClueCommunicateRecord
     */
    public function autoWhereDhExe($where)
    {
        return $this->where($where)->update(['is_f' => 0, 'f_uid' => 0]);
    }


    /**
     * 自动回收
     * @param $where
     * @return \app\model\ClueCommunicateRecord
     */
    public function autoWhereHsExe($where)
    {
        return $this->where($where)->update(['is_f' => 0, 'f_uid' => 0, 'is_del' => 1]);
    }

    /**
     * 通过id查询单条信息
     * @param $id
     * @return array|null
     */
    public function getIdFindModel($id)
    {
        $data = $this->where (['id'=>$id])->findOrEmpty ();
        if (!$data->isEmpty ()){
            return $data->toArray ();
        }
        return null;
    }

    public function delWhereDataModel($id,$communicate_content,$communicate_uid,$type,$communicate_date)
    {
        $this->where ([
            'clue_p_id' => $id,
            'communicate_content' => $communicate_content,
            'communicate_uid' => $communicate_uid,
            'type'=>$type
        ])->whereTime ('next_communicate_date',$communicate_date)->delete ();
    }
}